import java.util.Arrays;

public class ReversNums {
    public static void main(String[] args) {
        int[] nums = new int[] {1,2,3,4};
        /*rotate(nums,5);*/
        move(nums,5);
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i]+" ");
        }
    }
    /*时间复杂度达到n的平方*/
    /*空间复杂的O(1)*/
    public static void rotate(int[] nums, int n) {
        for (int i = 0; i < n; i++) {
            int k = nums[nums.length-1];
            for (int j = nums.length-1; j > 0; j--) {
                nums[j] = nums[j-1];
            }
            nums[0] = k;
        }
    }
    /*空间复杂度O(n)*/
    public static void move(int[] nums,int k) {
        int[] array = new int[k];
        int j = nums.length;
        if(k < nums.length) {
            //把后面k个数装起来
            for (int i = 0; i < k; i++) {
                array[i] = nums[j-k];
                j++;
            }
            //把剩下的数字向后移动k个位置
            for (int i = nums.length-k-1; i >= 0; i--) {
                nums[i+k] = nums[i];
            }
            for (int i = 0; i < k; i++) {
                nums[i] = array[i];
            }
        }
        else {
            for (int i = 0; i < k; i++) {
                int n = nums[nums.length-1];
                for (int f = nums.length-1; f > 0; f--) {
                    nums[f] = nums[f-1];
                }
                nums[0] = n;
            }
        }
    }
}
